﻿using System;
using System.Collections.Generic;
using System.Web;
using PayRoller.Code;
using System.Web.SessionState;
using System.Web.Profile;
using System.Web.UI.WebControls;
using System.Data;
using HySoft.Cash.Business;
using HySoft.Cash.Structure;
using AjaxControlToolkit;
using System.Web.UI.HtmlControls;
using PayRoller.UControls;

namespace PayRoller.Member
{
    public class EmpAdd : MemberParent, IRequiresSessionState
    {
        // Fields
        protected Button bnSelect;
        protected Button Button1;
        protected Button Button2;
        protected Button Button3;
        protected Button CancelButton;
        public DataTable dtBank = new DataTable();
        protected Label lblTip;
        protected ModalPopupExtender ModalPopupExtender;
        protected Panel Panel1;
        protected Panel Panel3;
        protected TextBox txtAccountName;
        protected TextBox txtAccountNum;
        protected TextBox txtAddress1;
        protected TextBox txtAddress2;
        protected TextBox txtBankName;
        protected TextBox txtBranch;
        protected TextBox txtBsb;
        protected TextBox txtEmpName;
        protected HtmlInputHidden txtEmpSid;
        protected TXTFormatDate txtNextPayday;
        protected TextBox txtOrgEmpSid;
        protected TextBox txtPayFrequency;
        protected TextBox txtPayReference;
        protected TextBox txtTaxFileNumber;

        // Methods
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                TempinfoBN obn = new TempinfoBN(this.Page);
                obn.QueryOrgSid(Convert.ToInt32(base.OrgSid));
                obn.QueryOrgEmpSid(this.txtOrgEmpSid.Text);
                if (obn.GetList().Rows.Count <= 0)
                {
                    TempinfoDT detail = new TempinfoDT();
                    detail.OrgSid = Convert.ToInt32(base.OrgSid);
                    detail.OrgEmpSid = this.txtOrgEmpSid.Text;
                    detail.EmpName = this.txtEmpName.Text;
                    detail.TaxFileNumber = this.txtTaxFileNumber.Text;
                    detail.NextPayday = this.txtNextPayday.Value;
                    detail.PayFrequency = this.txtPayFrequency.Text;
                    detail.Address1 = this.txtAddress1.Text;
                    detail.Address2 = this.txtAddress2.Text;
                    detail.IsValid = 1;
                    int empSid = obn.AddId(detail);
                    this.txtEmpSid.Value = empSid.ToString();
                    if (empSid > 0)
                    {
                        new OpeCommon().AddUptReport(empSid, "0001", Convert.ToInt32(base.OrgSid));
                        base.ClientScript.RegisterStartupScript(base.GetType(), "", "<script>alert('Success!');</script>");
                    }
                }
                obn.Dispose();
                obn = null;
            }
            catch (Exception exception)
            {
                this.lblTip.Text = exception.Message;
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (this.txtEmpSid.Value.Trim() != "")
            {
                TbankinfoBN obn = new TbankinfoBN(this.Page);
                obn.QueryEmpSid(Convert.ToInt32(this.txtEmpSid.Value));
                obn.QueryAccountNum(this.txtAccountNum.Text);
                if (obn.GetList().Rows.Count <= 0)
                {
                    TbankinfoDT detail = new TbankinfoDT();
                    detail.EmpSid = Convert.ToInt32(this.txtEmpSid.Value);
                    detail.BankName = this.txtBankName.Text;
                    detail.Branch = this.txtBranch.Text;
                    detail.AccountName = this.txtAccountName.Text;
                    detail.AccountNum = this.txtAccountNum.Text;
                    detail.Bsb = this.txtBsb.Text;
                    detail.PayReference = this.txtPayReference.Text;
                    if (obn.Add(detail))
                    {
                        new OpeCommon().AddUptReport(Convert.ToInt32(this.txtEmpSid.Value), "0004", Convert.ToInt32(base.OrgSid));
                    }
                    obn.Dispose();
                    obn = null;
                    this.GetBankList();
                    this.txtBankName.Text = "";
                    this.txtBranch.Text = "";
                    this.txtAccountName.Text = "";
                    this.txtAccountNum.Text = "";
                    this.txtBsb.Text = "";
                    this.txtPayReference.Text = "";
                }
            }
        }

        public void GetBankList()
        {
            if (this.txtEmpSid.Value.Trim() != "")
            {
                TbankinfoBN obn = new TbankinfoBN(this.Page);
                obn.QueryEmpSid(Convert.ToInt32(this.txtEmpSid.Value));
                this.dtBank = obn.GetList();
                obn.Dispose();
                obn = null;
            }
        }

        public void GetEmpDetail(string EmpSid)
        {
            TempinfoBN obn = new TempinfoBN(this.Page);
            obn.QuerySid(Convert.ToInt32(EmpSid));
            DataTable list = obn.GetList();
            obn.Dispose();
            obn = null;
            if (list.Rows.Count > 0)
            {
                this.txtOrgEmpSid.Text = list.Rows[0]["OrgEmpSid"].ToString();
                this.txtEmpName.Text = list.Rows[0]["EmpName"].ToString();
                this.txtTaxFileNumber.Text = list.Rows[0]["TaxFileNumber"].ToString();
                this.txtNextPayday.Text = list.Rows[0]["NextPayday"].ToString();
                this.txtPayFrequency.Text = list.Rows[0]["PayFrequency"].ToString();
                this.txtAddress1.Text = list.Rows[0]["Address1"].ToString();
                this.txtAddress2.Text = list.Rows[0]["Address2"].ToString();
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.CheckLogin();
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Button1.Attributes.Add("onclick", "return Check();");
            this.Button2.Attributes.Add("onclick", "return Check1();");
            if (base.Request["EmpSid"] != null)
            {
                this.txtEmpSid.Value = base.Request["EmpSid"].ToString();
                this.GetEmpDetail(this.txtEmpSid.Value);
                if (base.Request["BankSid"] != null)
                {
                    TbankinfoBN obn = new TbankinfoBN(this.Page);
                    obn.Delete(Convert.ToInt32(base.Request["BankSid"]));
                    obn.Dispose();
                    obn = null;
                }
            }
            this.GetBankList();
        }

        // Properties
        protected HttpApplication ApplicationInstance
        {
            get
            {
                return this.Context.ApplicationInstance;
            }
        }

        protected DefaultProfile Profile
        {
            get
            {
                return (DefaultProfile)this.Context.Profile;
            }
        }
    }

}
